﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using FrienDev.Data;

public partial class Controls_Message_ReceiveMessageDetails : System.Web.UI.UserControl
{
    protected string messageId;
    protected void Page_Load(object sender, EventArgs e)
    {

        messageId = GetQueryStringValue("messageId", "0");
        IEnumerable<Message> ds_ReceiveDetails = MessageRules.Instance.GetCurrentUserAssMessage(messageId);
        this.rpt_ReceiveDetails.DataSource = ds_ReceiveDetails;
        this.rpt_ReceiveDetails.DataBind();
        MessageRules.Instance.SignReadMessageByMessageId(messageId);

    }

    protected void rpt_ReceiveDetails_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Message message = e.Item.DataItem as Message;
        Literal temp_ltShortMessage = e.Item.FindControl("lt_SendedShortMessage") as Literal;
        temp_ltShortMessage.Text = message.MessageContent;
        HyperLink temp_hySender = e.Item.FindControl("hy_SUserName") as HyperLink;
        temp_hySender.Text = UserRules.Instance.GetUserByUserId(message.SenderId).FullName;
        temp_hySender.NavigateUrl = ApiHelper.Instance.GetUserHomepageLink(message.SenderId);
        Literal temp_ltSendDateTime = e.Item.FindControl("lt_SendTimeBySender") as Literal;
        temp_ltSendDateTime.Text = message.SendDateTime.ToShortDateString();
        Image temp_RMPhoto = e.Item.FindControl("img_RMPhoto") as Image;
        temp_RMPhoto.ImageUrl = ApiHelper.Instance.GetUserLogoUrl(message.User.UserId);

    }
    protected void btn_Reply_Click(object sender, EventArgs e)
    {
        string m_ReceiverId = MessageRules.Instance.GetReplyReceiverUserId(messageId);
        string m_ParentId = MessageRules.Instance.GetMessageObjectByMessageId(int.Parse(messageId)).ParentId.ToString();
        MessageRules.Instance.ReplyMessage(m_ReceiverId, this.txt_ReplyContent.Text, m_ParentId);
        Response.Redirect("/Home/SendMessage.aspx");
     
    }

    protected void lbt_DeleteSession_Click(object sender, EventArgs e)
    {
        MessageRules.Instance.DeleteSessionMessageByMessageId(messageId);
        Response.Redirect("/Home/SendMessage.aspx");
    }

    #region Protected Method
    protected int GetQueryStringValue(string QueryName, int nullValue)
    {

        if (string.IsNullOrEmpty(this.Page.Request.QueryString[QueryName]))
        {
            return nullValue;
        }
        return Convert.ToInt32(this.Page.Request.QueryString[QueryName]);
    }
    protected string GetQueryStringValue(string QueryName, string nullValue)
    {

        if (string.IsNullOrEmpty(this.Page.Request.QueryString[QueryName]))
        {
            return nullValue;
        }
        return this.Page.Request.QueryString[QueryName];
    }

    protected bool GetQueryStringValue(string QueryName, bool nullValue)
    {
        if (string.IsNullOrEmpty(this.Page.Request.QueryString[QueryName]))
        {
            return nullValue;
        }
        return Convert.ToBoolean(this.Page.Request.QueryString[QueryName]);
    }
    #endregion
}
